<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class PersonalMessage extends IndexController
{
    public function index()
    {


        $messagetomy = Db::field('u.icon,u.nickname,m.content,m.ptime,m.title,m.use_id,m.id')
        ->table('bbs_user u, bbs_message m')
        ->where('u.id = m.use_id')
        ->where('m.gid',input('session.user.id'))
        ->where('m.g_status','1')
        ->select();
        $this->assign('messagetomy',$messagetomy);



        $mytomessage = Db::field('u.icon,u.nickname,m.content,m.ptime,m.title,m.gid,m.id')
        ->table('bbs_user u, bbs_message m')
        ->where('u.id = m.gid')
        ->where('m.use_id',input('session.user.id'))
        ->where('m.use_status','1')
        ->select();
        $this->assign('mytomessage',$mytomessage);
    	
        return view('personal_message/index');
    }



    public function delmessage()
    {

        $id = input('id');
        $result = db('message')->where('id',$id)->update(['g_status'=>'0']);
        if($result){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败了');
        }
    }


    public function delmessage1()
    {

        $id = input('id');
        $result = db('message')->where('id',$id)->update(['use_status'=>'0']);
        if($result){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败了');
        }
    }



    public function addmessage()
    {

        return view('personal_message/add');
    }


    public function doaddmessage()
    {
        if (request()->isPost()) {
            $use_id = db('user')->field('id')->where('nickname',input('nickname'))->find();
            if(!$use_id){
                return $this->error('用户不存在'); 
            }

            $data = ['title' => input('title'), 'content' => input('content'), 'ptime' => date("Y-m-d H:i:s"), 'use_id' => input('session.user.id'), 'gid' => $use_id['id']];

            $validate = validate('Message');
            if ($validate->check($data)) {
                if(db('message')->insert($data)){
                    return $this->success('信息发送成功');
                }else{
                    return $this->error('信息发送失败');
                }

            }
            return $this->error($validate->getError());
 
        }
        return $this->fetch();
  

    }





}